package com.xbongbong.schedule.task;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.schedule.pojo.dto.ScheduleTaskActionDTO;
import com.xbongbong.schedule.pojo.vo.ScheduleTaskActionVO;
import com.xbongbong.schedule.task.actions.TaskActionInterface;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 任务执行器 上下文策略
 * @author zheng.li
 **/
@Component
public class ScheduleTaskContext {

    @Resource
    private ApplicationContext applicationContext;

    /**
     * 获取最终执行器
     * @param scheduleTaskType 延时任务类型
     * @return  获取最终执行器
     * @throws XbbException
     */
    private TaskActionInterface getTaskExecutor(Integer scheduleTaskType) throws XbbException {
        TaskTypeEnum taskTypeEnum = TaskTypeEnum.getByType(scheduleTaskType);
        if (taskTypeEnum == null) {
            throw new XbbException(-1,"不支持的任务类型！");
        }
        return applicationContext.getBean(taskTypeEnum.getExecutorClass());
    }

    /**
     * 延时任务执行
     * @param scheduleTaskActionDTO 入参
     * @param lang 语言包
     * @return 处理结果
     * @throws XbbException 系统异常
     */
    public XbbResponse<ScheduleTaskActionVO> scheduleTaskAction(ScheduleTaskActionDTO scheduleTaskActionDTO,String lang) throws XbbException {
        Integer scheduleTaskType = scheduleTaskActionDTO.getScheduleTaskEntity().getScheduleType();
        TaskActionInterface taskActionInterface = getTaskExecutor(scheduleTaskType);
        return taskActionInterface.scheduleTaskAction(scheduleTaskActionDTO,lang);
    }


}
